package com.naiterui.ehp.bs.agent.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.agent.service.IAreaService;
import com.naiterui.ehp.bs.agent.vo.AreaInfoVO;
import com.naiterui.ehp.bs.agent.vo.ProvinceItemVO;
import com.naiterui.ehp.bs.agent.vo.TreeDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.AGENT + "区域相关接口"})
@AllArgsConstructor
@RestController
@RequestMapping("/area")
public class AgentAreaController {
	private final IAreaService areaService;

	@GetMapping("change")
	@ApiOperation("切换经理身份")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaId", value = "区域ID", dataType = "long", paramType = "query", required = true),
	})
	public void changeArea(@RequestHeader Long agentId, Long areaId) throws BusinessException {
		if (areaId == null){
			throw new BusinessException(BaseExceptionCodes.PARAM_ERROR);
		}
		areaService.changeArea(agentId, areaId);
	}

	@GetMapping("info")
	@ApiOperation("统计单个大区或省区汇总")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "year", value = "年份", dataType = "string", paramType = "query", required = false),
			@ApiImplicitParam(name = "month", value = "月份", dataType = "string", paramType = "query", required = false),
	})
	public AreaInfoVO getAreaInfo(@RequestHeader Long agentId, String year, String month) throws BusinessException {
		if (agentId == null){
			throw new BusinessException(BaseExceptionCodes.PARAM_ERROR);
		}
		return areaService.getAreaInfo(agentId, year, month);
	}

	@GetMapping("province/list")
	@ApiOperation("查询省区统计列表")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "year", value = "年份 YYYY", dataType = "string", paramType = "query", required = false),
        @ApiImplicitParam(name = "month", value = "月份 MM", dataType = "string", paramType = "query",
            required = false),
	})
    public List<ProvinceItemVO> findProvinceList(@RequestHeader Long agentId, String year, String month)
        throws BusinessException {
        return areaService.findProvinceList(agentId, year, month);
	}

	@GetMapping("tree")
	@ApiOperation("大区和省区树形结构数据")
	public List<TreeDataVO> areaTree() {
		return areaService.findTreeData();
	}

}
